package itsm.isperp.framework.data.domain;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.data.mybatis.plugin.JqGridResponseData;
import itsm.isperp.framework.utils.JsonBuilder;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * Spring Data与Mybatis的扩展类, 通过java.util.List可以得到Mybatis的一个返回列表。 并且列表部分是不确定的。
 * 
 * @author lizx
 * @date 2013-1-15
 * @version V1.0
 */
public class PageResponse<T> implements Serializable, Page<T> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 当前页数
	 */
	protected int page;
	/**
	 * 总页数
	 */
	protected int total;
	/**
	 * 总记录数
	 */
	protected int records;
	/**
	 * 当前页数据
	 */
	protected List<T> content;

	private ITreeNode[] menudata;

	public PageResponse() {
	}

	/**
	 * 通过一个Page分页构造
	 * 
	 * @param page
	 *            分页
	 */
	public PageResponse(final Page<T> page) {
		this.page = page.getNumber();
		this.total = page.getTotalPages();
		this.records = (int) page.getTotalElements();
		this.content = page.getContent();
	}

	public PageResponse(final JqGridResponseData<T> page) {
		this.page = page.getNumber();
		this.total = page.getTotalPages();
		this.records = (int) page.getTotalElements();
		this.content = page.getContent();
		this.menudata = page.getMenudata();
	}

	/**
	 * 构造返回数据
	 * 
	 * @param page
	 *            页码
	 * @param total
	 *            总页数
	 * @param records
	 *            总记录数
	 * @param rows
	 *            数据行
	 */
	public PageResponse(int page, int total, int records, List<T> content) {
		this.page = page;
		this.total = total;
		this.records = records;
		this.content = content;
	}

	public ITreeNode[] getMenudata() {
		return menudata;
	}

	public void setMenudata(ITreeNode[] menudata) {
		this.menudata = menudata;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getRecords() {
		return records;
	}

	public void setRecords(int records) {
		this.records = records;
	}

	public void setContent(List<T> content) {
		this.content = content;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[\"page\":");
		builder.append(page);
		builder.append(", \"total\":");
		builder.append(total);
		builder.append(", \"records\":");
		builder.append(records);
		builder.append(", ");
		if (content != null) {
			builder.append("\"content\":");
			builder.append(JsonBuilder.buildNormalBinder().toJson(content));
		}
		builder.append("]");
		return builder.toString();
	}

	@Override
	public int getNumber() {
		return this.page;
	}

	@Override
	public int getSize() {
		return this.records;
	}

	@Override
	public int getTotalPages() {
		return this.total;
	}

	@Override
	public int getNumberOfElements() {
		return this.content.size();
	}

	@Override
	public long getTotalElements() {
		return (long) this.records;
	}

	@Override
	public Iterator<T> iterator() {
		return this.content.iterator();
	}

	@Override
	public List<T> getContent() {
		return this.content;
	}

	@Override
	public boolean hasContent() {
		return true;
	}

	@Override
	public Sort getSort() {

		return null;
	}

	@Override
	public Pageable nextPageable() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pageable previousPageable() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean hasPrevious() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isFirst() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isLast() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public <S> Page<S> map(Converter<? super T, ? extends S> arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}